package com.itheima.edu.practice.demo;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

public class TreeMapDemo {
    //创建一个TreeMap集合，键是学生对象，值是籍贯，存储三个键值对元素并遍历
    public static void main(String[] args) {
        //比较器比较年龄和名字字典排序
        Map<Student,String> map = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result = o1.getAge() - o2.getAge();
                result = result == 0 ? o1.getName().compareTo(o2.getName()):result;
                return result;
            }
        });
        map.put(new Student("龙门",18),"益阳");
        map.put(new Student("高歌",29),"长沙");
        map.put(new Student("安歇",25),"怀化");

        //遍历输出
        map.forEach(
                (Student student , String s )->{
                    System.out.println(student + "---" + s);
                }
        );
    }
}
